import Vue from 'vue'
import VueRouter from 'vue-router'
import {getUserInfo} from '../store/user/user'
import store from '../store'
import { Toast } from 'vant';

Vue.use(VueRouter)
// 防止路由重复点击报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  // 默认加载界面
  {
    path: '/',
    redirect: '/login'
  },
  // 登录页面界面
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue')
  },
  // 注册页面路由
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register.vue')
  },
  // 总路由
  {
    path: '/manager',
    name: 'manager',
    component: () => import('@/views/Manager.vue'),
    children: [
      // 配置子路由的默认加载界面
      {
        path: '/manager',
        redirect: '/manager/home'
      },
      // 首页路由
      {
        path: 'home',
        component: () => import('@/views/Home/Home.vue')
      },
      // 订单路由
      {
        path: 'article',
        component: () => import('@/views/Article/Article.vue')
      },
      // 订单详情路由
      {
        path: 'detail',
        component: () => import('@/views/Article/Detail.vue')
      },
      // 预约详情
      {
        path: 'Reservation',
        component: () => import('@/views/chauffeur/Details.vue')
      },
      // 练车记录
      {
        path: 'Record',
        component: () => import('@/views/chauffeur/Record.vue')
      },
      // 我的分享
      {
        path: 'myArticle',
        component: () => import('@/views/Article/myArticle.vue')
      },
      // 订单评论
      {
        path: 'comment',
        component: () => import('@/views/Article/Comment.vue'),
      },
      // 帮助路由
      {
        path: 'chauffeur',
        component: () => import('@/views/chauffeur/chauffeur.vue')
      },
      // 我的页面路由
      {
        path: 'mine',
        component: () => import('@/views/Mine/Mine.vue')
      },
      // 产品页面的路由
      {
        path: 'product',
        component: () => import('@/views/Product/Product.vue')
      },
      // 提交订单的路由
      {
        path: 'orderConfirm',
        component: () => import('@/views/orderConfirm/orderConfirm.vue')
      },
      // 我的地址路由
      {
        path: 'address',
        component: () => import('@/views/Address/Address.vue'),
      },
      {
        path: 'news',
        component: () => import('@/views/News/News.vue')
      },
      {
        path: 'pinglun',
        component: () => import('@/views/Mine/Pinglun.vue'),
      },
      {
        path: 'mydetail',
        component: () => import('@/views/Mine/MyDetail.vue'),
      },
      {
        path: 'orderDetailDesc',
        component: () => import('@/views/Article/orderDetailDesc.vue'),
      },

    ]
  },

]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫 判断有无token
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach(async(to, from, next) => {
  //若要跳转的页面是登录界面
  if (to.path === '/login') {
    //直接跳转
    next();
  } else { //若想要跳转其他页面
    //获取本地存储的token值
    let token = localStorage.getItem('token');
    //若token为空则验证不成功，跳转到登录页面
    if (token === 'null' || token === '') {
      next('/login');
    } else {
     await store.dispatch('user/getUserInfo')
      const role = store.state.user.userInfo.role
      if(role !== "trainees"){
        next('/login');
        Toast.fail('请使用教练员用户账号登录');
      }else{
        next();
      }
    }
  }
});

export default router